﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Xml.XPath;

namespace GeoParser
{
	/// <summary>
	/// A single point along a <see cref="Track"/>.
	/// </summary>
	[Immutable]
	public class TrackPoint : GeoPoint, IComparable<TrackPoint>
	{
		private readonly XElement m_Element;

		public TrackPoint(XElement element)
		{
			if (element == null)
				throw new ArgumentNullException("element");

			m_Element = element;
			Latitude = ParseDegrees(element.Attribute("lat").Value);
			Longitude = ParseDegrees(element.Attribute("lon").Value);
		}

		public DateTime Time
		{
			get { return DateTime.Parse(m_Element.Element(m_Element.Name.Namespace + "time").Value); }
		}

		public static bool Equals(TrackPoint a, TrackPoint b)
		{
			if (a == null)
				return b == null;
			if (b == null)
				return false;
			return GeoPoint.Equals(a, b) && a.Time.Equals(b.Time);
		}

		#region IComparable<TrackPoint> Members

		public int CompareTo(TrackPoint other)
		{
			return this.Time.CompareTo(other.Time);
		}

		#endregion
	}
}
